package com.seeyon.apps.thirdSso.controller;

import com.seeyon.apps.thirdSso.util.HttpUtils;
import com.seeyon.ctp.util.json.JSONUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
public class IntegratedController extends HttpServlet {
    private static Log log = LogFactory.getLog(IntegratedController.class);
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out=response.getWriter();
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        String sub = request.getParameter("sub");
        log.info("获取参数sub："+sub);
        String[] s = sub.split("\\|");
        String id = s[1];
        long memberIdByCode = HttpUtils.getMemberIdByCode(id);
        String json = "";
        if (memberIdByCode != 0){
            json = JSONUtil.toJSONString(allow());
        }else {
            json = JSONUtil.toJSONString(error());
        }
        log.info("interSync返回参数："+json);
        out.write(json.toString());
        out.close();
    }
//    @AjaxAccess
//    @NeedlessCheckLogin
//    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception{
//        request.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json;charset=UTF-8");
//        String sub = request.getParameter("sub");
//        log.info("获取参数sub："+sub);
//        String[] s = sub.split("\\|");
//        String id = s[1];
//        String memberId = HttpUtils.getMemberIdByCode(id);
//        String json = "";
//        if (memberId != null&&!memberId.isEmpty()){
//            json = JSONUtil.toJSONString(allow());
//        }else {
//            json = JSONUtil.toJSONString(error());
//        }
//        log.info("interSync返回参数："+json);
//        PrintWriter out=response.getWriter();
//        out.write(json.toString());
//        out.close();
//        return null;
//    }
    private Map<String, Object> error() {
        Map<String, Object> data = new HashMap<>();
        data.put("accessible",false);
        Map<String, Object> jsonObject =new HashMap<>();
        jsonObject.put("status", "C00501");
        jsonObject.put("msg", "内部异常");
        jsonObject.put("data",data);
        return jsonObject;
    }
    private Map<String, Object> allow() {
        Map<String, Object> data = new HashMap<>();
        data.put("accessible",true);
        Map<String, Object> jsonObject = new HashMap<>();
        jsonObject.put("status", "C00000");
        jsonObject.put("msg", "操作成功");
        jsonObject.put("data",data);
        return jsonObject;
    }
}
